package net.akkz.server.impl;

import java.util.ArrayList;

import net.akkz.dao.AnswerDao;
import net.akkz.dao.QuestionDao;
import net.akkz.dbFactory.DbManage;
import net.akkz.dbFactory.Session;
import net.akkz.model.Answer;
import net.akkz.model.Question;
import net.akkz.server.QuestionServer;

public class QuestionServerImpl implements QuestionServer
{
	private QuestionDao questionDao;
	private AnswerDao answerDao;
	private DbManage dbManage;

	@Override
	public void addQuestion(Question question)
	{
		Session session = dbManage.createSession();

		try
		{
			questionDao.addQuestion(session, question);

			session.commit();
		}
		catch (Exception e)
		{
			session.rollback();
		}
	}

	@Override
	public ArrayList<Question> findQuestion(int start, int end)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<Question> questions = questionDao.findQuestion(session, start, end);

			session.commit();

			return questions;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public ArrayList<Question> findQuestionOrderByGoodNum(int start, int end)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<Question> questions = questionDao.findQuestionOrderByGoodNum(session, start, end);

			session.commit();

			return questions;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public ArrayList<Question> findQuestionWithKind(int start, int end, int kind)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<Question> questions = questionDao.findQuestionWithKind(session, start, end, kind);

			session.commit();

			return questions;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public ArrayList<Question> findQuestionOrderByGoodNumWithKind(int start, int end, int kind)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<Question> questions = questionDao.findQuestionOrderByGoodNumWithKind(session, start, end, kind);

			session.commit();

			return questions;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public ArrayList<Question> findQuestionByStudentId(int studentId)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<Question> questions = questionDao.findQuestionByStudentId(session, studentId);

			session.commit();

			return questions;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public Question getQuestion(int id)
	{
		Session session = dbManage.createSession();

		try
		{
			Question question = questionDao.getQuestion(session, id);

			session.commit();

			return question;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public void deleteQuestion(int id, int studentid)
	{
		Session session = dbManage.createSession();

		try
		{
			Question question = questionDao.getQuestion(session, id);
			
			if(question.getStudentid() == studentid)
				questionDao.deleteQuestion(session, id);

			session.commit();
		}
		catch (Exception e)
		{
			session.rollback();
		}
	}

	@Override
	public void addAnswer(Answer answer)
	{
		Session session = dbManage.createSession();

		try
		{
			answerDao.addAnswer(session, answer);

			session.commit();
		}
		catch (Exception e)
		{
			session.rollback();
		}
	}

	@Override
	public ArrayList<Answer> findAnswerByQuestionId(int questionId)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<Answer> answers = answerDao.findAnswerByQuestionId(session, questionId);

			session.commit();

			return answers;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public void deleteAnswer(int id)
	{
		Session session = dbManage.createSession();

		try
		{
			answerDao.deleteAnswer(session, id);

			session.commit();
		}
		catch (Exception e)
		{
			session.rollback();
		}
	}

	public void setQuestionDao(QuestionDao questionDao)
	{
		this.questionDao = questionDao;
	}

	public void setAnswerDao(AnswerDao answerDao)
	{
		this.answerDao = answerDao;
	}

	public void setDbManage(DbManage dbManage)
	{
		this.dbManage = dbManage;
	}

}
